package com.wnxy.pms.order.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wnxy.pms.order.core.constant.OrderConstant;
import com.wnxy.pms.order.core.dto.OrderDto;
import com.wnxy.pms.order.core.entity.Order;
import com.wnxy.pms.order.core.entity.OrderItem;
import com.wnxy.pms.order.core.exception.Asserts;
import com.wnxy.pms.order.core.mapper.OrderItemMapper;
import com.wnxy.pms.order.core.mapper.OrderMapper;
import com.wnxy.pms.order.core.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.pms.order.core.vo.OrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author Jet
 * @since 2022-06-09
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired(required = false)
    private OrderItemMapper orderItemMapper;

    @Override
    public String createOrder(OrderDto orderDto) {
        // 保存订单:pms_order
        Order order= new Order();

        // 用户id
        order.setUserId(orderDto.getUserId());

        // 设置订单编号
        order.setOrderSn(UUID.randomUUID().toString());

        // 活动价格
        order.setPromotionAmount(orderDto.getPromotionPrice());

        // 创建时间
        order.setCreateTime(new Date());

        // 订单状态：未支付
        order.setStatus(OrderConstant.ORDER_STATUS_UNPAY);

        // 支付金额
        order.setPayAmount(orderDto.getPromotionPrice());

        // 总金额
        order.setTotalAmount(orderDto.getPromotionPrice());

        // 订单类型：秒杀订单
        order.setOrderType(OrderConstant.ORDER_SECKILL_TYPE);

        // 订单来源
        order.setSourceType(OrderConstant.ORDER_SOURCE_PC);

        // 保存订单
        baseMapper.insert(order);

        // 保存订单明细:pms_order_item
        OrderItem orderItem = new OrderItem();
        // 设置订单明细，关联订单id
        orderItem.setOrderId(order.getId());
        // 设置订单编号
        orderItem.setOrderSn(order.getOrderSn());
        // 商品编号
        orderItem.setProductId(orderDto.getProductId());
        // 商品名称、图片、价格、购买数量
        orderItem.setProductName(orderDto.getProductName());
        orderItem.setProductPic(orderDto.getProductPic());
        orderItem.setProductPrice(orderDto.getPrice());
        orderItem.setProductQuantity(1);
        orderItem.setPromotionAmount(orderDto.getPromotionPrice());
        // 保存订单明细
        orderItemMapper.insert(orderItem);
        return order.getOrderSn();
    }

    @Override
    public OrderVo findByOrderSn(String orderSn) {
        LambdaQueryWrapper<Order> wrapper = Wrappers.lambdaQuery();
        //查询部分字段
        wrapper.select(Order::getOrderSn,Order::getTotalAmount,Order::getStatus);
        wrapper.eq(Order::getOrderSn,orderSn);
        Map<String,Object> result = this.getMap(wrapper);
        OrderVo vo = new OrderVo();
        //BeanUtils.copyProperties(result,vo);//不会下划线转驼峰
        //return vo;
        return BeanUtil.copyProperties(result,OrderVo.class);
    }

    @Override
    public Integer updateByOrderSn(String orderSn) {
        Order order = baseMapper.selectByOrderSn(orderSn);
        if(Objects.isNull(order)){
            Asserts.failed("订单不存在");
        }
        // 待发货
        int count= baseMapper.updateByOrderSn(orderSn,OrderConstant.ORDER_STATUS_UNSHIP);
        if(count<0){
            Asserts.failed("订单更新失败");
        }
        return OrderConstant.ORDER_STATUS_UNSHIP;
    }
}
